﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;
using StudentManage.Common;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class TeacherEdit : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            //DropBind();
            if (Request.QueryString["XH"] != null)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            string XH=Request.QueryString["XH"].ToString();
            xy_jgxx md = xy_jgxx.Where("ZGH=@0", XH).First();
            this.txt_XH.Text = md.ZGH;
            this.txt_CARDID.Text = md.CARDID.ToString();
            this.Drop_Sex.SelectedValue = md.XB;

            this.SelectDept.DeptID = md.BMDM;
            this.SelectDept.DeptName = md.BMMC;
            this.txt_XM.Text = md.XM;
        }

        private bool IsExistsXH(string XH)
        {
            return xy_jgxx.Where("ZGH=@0", XH).Count() > 0;
        }

        private void AddStudent()
        {
            if (IsExistsXH(this.txt_XH.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('该工号已存在！'); });</script>");
                return;
            }

            xy_jgxx md = new xy_jgxx();
            md.ZGH = this.txt_XH.Text;
            md.CARDID = Convert.ToInt32(this.txt_CARDID.Text.Trim() == "" ? "0" : this.txt_CARDID.Text.Trim());
            md.XM = this.txt_XM.Text;
            md.XB = this.Drop_Sex.SelectedValue;

            xy_bmxbm bm=xy_bmxbm.Where("BMDM=@0",this.SelectDept.DeptID).First();
            if(bm.BMFQID!="03")
            {
                md.XYMC = xy_bmxbm.Where("BMDM=@0", bm.BMFQID).First().BMMC;
            }
            md.BMDM = this.SelectDept.DeptID;
            md.BMMC = this.SelectDept.DeptName;

            md.IdKey = Guid.NewGuid();
            md.IsEdit = false;
            xy_jgxx.Insert(md);

            UserList umd = new UserList();
            umd.XH = md.ZGH;
            umd.IsStudent = true;
            umd.UserName = md.ZGH;
            umd.PassWord = Utility.GetMD5PassWord(md.ZGH, "");
            umd.UName = md.XM;
            umd.DataRole = 12;//部门管理员
            umd.RoleCode = "463915fcee193102";
            umd.RoleName = "部门管理员";
            umd.AddTime = DateTime.Now;
            UserList.Insert(umd);
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加成功！');window.parent.jsRefreshList();</script>");
        }

        private void EditStudent()
        {
            string XH = Request.QueryString["XH"].ToString();
            if (this.txt_XH.Text.Trim() != XH)
            {
                if (IsExistsXH(this.txt_XH.Text.Trim()))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>$(document).ready(function () { alert('该学号已存在！'); });</script>");
                    return;
                }
            }

            xy_jgxx md = xy_jgxx.Where("ZGH=@0", XH).First();
            md.ZGH = this.txt_XH.Text;
            md.CARDID = Convert.ToInt32(this.txt_CARDID.Text.Trim() == "" ? "0" : this.txt_CARDID.Text.Trim());
            md.XM = this.txt_XM.Text;
            md.XB = this.Drop_Sex.SelectedValue;
            if (md.BMDM != this.SelectDept.DeptID)
            {
                md.IsEdit = true;
            }
            xy_bmxbm bm = xy_bmxbm.Where("BMDM=@0", this.SelectDept.DeptID).First();
            if (bm.BMFQID != "03")
            {
                md.XYMC = xy_bmxbm.Where("BMDM=@0", bm.BMFQID).First().BMMC;
            }
            md.BMDM = this.SelectDept.DeptID;
            md.BMMC = this.SelectDept.DeptName;
            xy_jgxx.Update(md, "ZGH=@0", XH);

            UserList Umd = UserList.Where("XH=@0 and IsStudent=@1", XH, false).FirstOrDefault();
            if (Umd != null)
            {
                if (this.txt_XH.Text.Trim() != XH)
                {
                    Umd.XH = md.ZGH;
                    Umd.UserName = md.ZGH;
                    Umd.AddTime = DateTime.Now;
                    UserList.Update(Umd, "id=@0", Umd.id);
                }
            }
            else
            {
                UserList umd = new UserList();
                umd.XH = md.ZGH;
                umd.IsStudent = false;
                umd.UserName = md.ZGH;
                umd.PassWord = Utility.GetMD5PassWord(md.ZGH, "");
                umd.UName = md.XM;
                umd.DataRole = 12;//部门管理员
                umd.RoleCode = "463915fcee193102";
                umd.RoleName = "部门管理员";
                umd.AddTime = DateTime.Now;
                UserList.Insert(umd);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('修改成功！');window.parent.jsRefreshList();</script>");//.location.href='TeacherList.aspx'
        }

        protected void Btn_SaveStudent_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["XH"] != null)
            {
                this.EditStudent();
            }
            else
            {
                this.AddStudent();  
            }
        }
    }
}